package com.geniusk.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ 的配置类
 */
@Configuration
public class MQConfig {

    // 定义交换机的名称
    public static final String MQTT_EXCHANGE = "mqtt_exchange";

    // 定义队列的名称
    public static final String MQTT_QUEUE = "mqtt_queue";

    // 统一定义路由key
    public static final String ROUTING_KEY_MQTT_SEND = "geniusk.mqtt.send.";

    // 创建交换机
    @Bean(MQTT_EXCHANGE)
    public Exchange exchange() {
        return ExchangeBuilder
                .topicExchange(MQTT_EXCHANGE)
                .durable(true)
                .build();
    }

    // 创建队列
    @Bean(MQTT_QUEUE)
    public Queue queue() {
        return QueueBuilder
                .durable(MQTT_QUEUE)
                .build();
    }

    // 创建绑定关系
    @Bean
    public Binding mqttBinding(@Qualifier(MQTT_EXCHANGE) Exchange exchange, @Qualifier(MQTT_QUEUE) Queue queue) {
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("geniusk.mqtt.#")
                .noargs();
    }

}